/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package data.Imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import data.Services.CommentDAOService;
import model.entity.Comment;
import model.entity.Post;
import model.entity.User;

/**
 *
 * @author ninkuhack
 */
public class CommentServiceDAOImp extends ConnectionManager implements CommentDAOService {

    private static CommentServiceDAOImp commentDAO;

    public static CommentServiceDAOImp getInstance() {
        if (commentDAO == null) {
            commentDAO = new CommentServiceDAOImp();
        }
        return commentDAO;
    }

    private Comment createNewComment(ResultSet rs) throws SQLException{
        Comment comment = new Comment();
        comment.setCommentID(rs.getInt("comment_id"));
        comment.setContent(rs.getString("content"));
        comment.setStatus(rs.getInt("status"));
        comment.setUser_id(UserServiceDAOImp.getInstance().getUserByID(rs.getInt("user_id")));
        comment.setPost_id(PostServiceDAOImp.getInstance().getPostByID(rs.getInt("post_id")));
        return comment;
    }
    
    @Override
    public List<Comment> getAllComment() {
        List<Comment> commentList = new ArrayList<>();
        try {
            Connection conn = ConnectionManager.getConnection();
            String SQL = "SELECT * FROM comments where status = 1";
            PreparedStatement ps = conn.prepareStatement(SQL);

            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    commentList.add(createNewComment(rs));
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return commentList;
    }

    @Override
    public boolean createComment(Comment comment) {
        try {
            Connection conn = ConnectionManager.getConnection();
            String SQL2 = "INSERT INTO comments(content, user_id, post_id, status) VALUES(?, ?, ?, ?)";
            PreparedStatement ps = conn.prepareStatement(SQL2);
            ps.setString(1, comment.getContent());
            ps.setObject(2, comment.getUser_id());
            ps.setObject(3, comment.getPost_id());
            ps.setInt(4, 0);
            ps.executeUpdate();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteComment(int commentID) {
        try {
            Connection conn = ConnectionManager.getConnection();
            String SQL = "update comments set status = 0 WHERE comment_id = ?";
            PreparedStatement ps = conn.prepareStatement(SQL);
            ps.setInt(1, commentID);
            ps.executeUpdate();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }
    
    @Override
    public boolean deleteCommentByUsedID(int usedID) {
        try {
            Connection conn = ConnectionManager.getConnection();
            String SQL = "update comments set status = 0 WHERE user_id = ?";
            PreparedStatement ps = conn.prepareStatement(SQL);
            ps.setInt(1, usedID);
            ps.executeUpdate();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteCommentByPostID(int postID) {
        try {
            Connection conn = ConnectionManager.getConnection();
            String SQL = "update comments set status = 0 WHERE post_id = ?";
            PreparedStatement ps = conn.prepareStatement(SQL);
            ps.setInt(1, postID);
            ps.executeUpdate();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Comment> findCommentUserID(int userID) {
        List<Comment> commentList = new ArrayList<>();
        try {
            Connection conn = ConnectionManager.getConnection();
            String SQL = "SELECT * FROM comments WHERE user_id = ? and status = 1";
            PreparedStatement ps = conn.prepareStatement(SQL);
            ps.setInt(1, userID);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    commentList.add(createNewComment(rs));
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return commentList;
    }

    @Override
    public List<Comment> findCommentPostID(int postID) {
        List<Comment> commentList = new ArrayList<>();
        try {
            Connection conn = ConnectionManager.getConnection();
            String SQL = "SELECT * FROM comments WHERE post_id = ? and status = 1";
            PreparedStatement ps = conn.prepareStatement(SQL);
            ps.setInt(1, postID);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    commentList.add(createNewComment(rs));
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return commentList;
    }

    @Override
    public Comment getCommentByID(int commentID) {
        Comment comment = new Comment();
        try {
            Connection conn = ConnectionManager.getConnection();
            String SQL = "SELECT * FROM comments WHERE comment_id = ? and status = 1";
            PreparedStatement ps = conn.prepareStatement(SQL);
            ps.setInt(1, commentID);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                comment = createNewComment(rs);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return comment;
    }

    @Override
    public boolean lockComment(Comment comment) {
        try {
            Connection conn = ConnectionManager.getConnection();
            String SQL = "UPDATE comments SET status = ? WHERE comment_id = ? ";
            PreparedStatement ps = conn.prepareStatement(SQL);
            ps.setInt(1, 1);
            ps.setInt(2, comment.getCommentID());
            ps.executeUpdate();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean restoreComment(Comment comment) {
        try {
            Connection conn = ConnectionManager.getConnection();
            String SQL = "UPDATE comments SET status = ? WHERE comment_id = ?";
            PreparedStatement ps = conn.prepareStatement(SQL);
            ps.setInt(1, 0);
            ps.setInt(2, comment.getCommentID());
            ps.executeUpdate();
            ps.close();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateComment(Comment comment) {
        try {
            Connection conn = ConnectionManager.getConnection();
            String SQL = "UPDATE comments SET content = ? WHERE comment_id = ?";
            PreparedStatement ps = conn.prepareStatement(SQL);
            ps.setString(1, comment.getContent());
            ps.setInt(2, comment.getCommentID());
            ps.executeUpdate();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }
}
